package cn.zmwh.portal.modules.index.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.exception.RRException;
import cn.zmwh.common.security.base.BaseUserDetails;
import cn.zmwh.common.security.base.UserService;
import cn.zmwh.common.security.pojo.dto.LoginDTO;
import cn.zmwh.portal.modules.index.mapper.TlUserMapper;
import cn.zmwh.portal.modules.index.pojo.entity.TlUser;
import cn.zmwh.portal.modules.index.pojo.vo.TlUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author dmzmwh
 * @since 2024-08-26
 */
@Slf4j
@Service
public class TlUserService extends ServiceImpl<TlUserMapper, TlUser> implements UserService, UserDetailsService {

    private UserDetails userToUserDetails(TlUser tlUser) {
        Integer enabled = tlUser.getEnabled() == null ? 1 : tlUser.getEnabled();
        if (enabled == 2) {
            throw new RRException("账号被禁用！");
        }
        Long id = tlUser.getId();
        String username = tlUser.getUserName();
        String nick = tlUser.getNick();
        BaseUserDetails userDetail = new BaseUserDetails(id, id, nick, username, "", new HashSet<>());
        return userDetail;
    }


    @Override
    public UserDetails loadUser(LoginDTO request) {
        TlUser tlUser = baseMapper.selectOne(Wrappers.<TlUser>lambdaQuery()
                .eq(StrUtil.isNotBlank(request.getMobile()), TlUser::getMobile, request.getMobile())
                .eq(StrUtil.isNotBlank(request.getEmail()), TlUser::getEmail, request.getEmail()));

        if (tlUser == null) {
            boolean create = request.isCreate();
            if (!create) {
                throw new RRException("用户不存在！");
            }
            //不存在默认创建
            tlUser = new TlUser();
            tlUser.setMobile(request.getMobile());
            tlUser.setEmail(request.getEmail());
            baseMapper.insert(tlUser);
        }

        return userToUserDetails(tlUser);
    }

    @Override
    public UserDetails loadUserById(Long id) {
        TlUser tlUser = baseMapper.selectById(id);
        if (tlUser == null) {
            throw new RRException("用户不存在！");
        }
        return userToUserDetails(tlUser);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        boolean email = Validator.isEmail(username);
        LambdaQueryWrapper<TlUser> eq = Wrappers.<TlUser>lambdaQuery();
        if (email) {
            eq.eq(TlUser::getEmail, username);
        } else {
            eq.eq(TlUser::getMobile, username);
        }
        TlUser tlUser = baseMapper.selectOne(eq);
        return userToUserDetails(tlUser);
    }

    public Result userInfo(Long id) {
        TlUser tlUser = baseMapper.selectById(id);
        if (tlUser == null) {
            return Result.failed(401, "用户不存在！");
        }
        TlUserVo tlUserVo = new TlUserVo();
        BeanUtils.copyProperties(tlUser, tlUserVo);


        return Result.ok(tlUserVo);
    }

}
